package library.and.xps.com.step.view.activity;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import library.and.xps.com.step.R;
import library.and.xps.com.step.util.AppCommond;
import library.and.xps.com.step.util.CommonUtils;
import library.and.xps.com.step.view.base.BaseActivity;
import library.and.xps.com.step.view.fragment.SearchFragment;

public class SearchActivity extends BaseActivity {

    SearchFragment searchFragment;
    ImageView iv_search;
    TextView tv_cancel;
    EditText et_content;

    @Override
    protected int getLayout() {
        return R.layout.activity_search;
    }

    @Override
    protected void setData() {
        searchFragment = new SearchFragment();
        CommonUtils.replaceFragment(R.id.search_container, searchFragment, getSupportFragmentManager());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void init() {
        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);
        et_content = r2v(R.id.et_content);
        iv_search = r2v(R.id.iv_search);
        tv_cancel = r2v(R.id.tv_cancel);
        iv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchFragment.doSearch(et_content.getText().toString(), 1);
            }
        });

        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceiveEvent(AppCommond a) {
        if (a != null) {
            if (null != a.getAcNameList() && a.getOperation() == AppCommond.CLOSE_ACTIVITY && a.getAcNameList().size() > 0)
                for (String activityName : a.getAcNameList()) {
                    if (activityName.equals(this.getClass().getSimpleName())) {
                        finish();
                        break;
                    }
                }
        }
    }
}
